/**
 * Copyright © 2018
 * 
 */
package com.adds.lvds.service.impl.log;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.adds.lvds.core.utils.ApplicationContextUtils;
import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.dao.impl.log.LogInfoMapper;
import com.adds.lvds.model.log.T_operation_log;
import com.adds.lvds.service.impl.Service;

/**
 * @author: zhangjian   
 * @date: 2018年7月30日 上午10:03:09
 * @desc TODO
 */
public class LogSaveService implements Service {
	private boolean isRun = false;

	private Thread logSaveService;
	
	private LogInfoMapper logInfoMapper = null;

	private static List<T_operation_log> logList = Collections
		.synchronizedList(new LinkedList<T_operation_log>());
	
	public void destroy() {
		logSaveService.interrupt();
		isRun = false;
	}

	public boolean initialize() {
		logSaveService = new Thread(this, "日志服务线程启动");
		logInfoMapper= (LogInfoMapper) ApplicationContextUtils.getBean("logInfoMapper");
		return true;
	}
	public boolean isOK() {
		return isRun;
	}

	public void startup() {
		isRun = true;
		logSaveService.start();
	}

	public void run() {
		while (isRun && !Thread.interrupted()) {
			try {
				if(!logList.isEmpty()) {
					T_operation_log t_log = logList.remove(0);
					logInfoMapper.addT_operation_log(t_log);
				}
				Thread.sleep(100);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void saveLog(String op,String method,String userName,String json) {
		T_operation_log t_operation_log=new T_operation_log();
		t_operation_log.setPk_log_id(CommUtils.getUUID());
		t_operation_log.setModule(op);
		t_operation_log.setLog_msg(json);
		t_operation_log.setUserName(userName);
		t_operation_log.setClassMethod(method);
		logList.add(t_operation_log);
	}
}
